package com.qf.device.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.device.entity.Device;
import com.qf.device.mapper.DeviceMapper;
import com.qf.device.service.DeviceService;
import com.qf.dto.PageDto;
import com.qf.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 设备表 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2023-10-26
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public PageResult<Device> pageInfo(PageDto<Device> pageDto) {
        String name = null;
        String deviceKey = null;
        Long deviceTypeId = null;
        Integer active = null;
        //条件判断
        if (!StringUtils.isEmpty(pageDto.getWhere().getName())){
            name = pageDto.getWhere().getName();
        }
        if (!StringUtils.isEmpty(pageDto.getWhere().getDeviceKey())){
            deviceKey = pageDto.getWhere().getDeviceKey();
        }
        if (!StringUtils.isEmpty(pageDto.getWhere().getDeviceTypeId())){
            deviceTypeId = pageDto.getWhere().getDeviceTypeId();
        }
        if (!StringUtils.isEmpty(pageDto.getWhere().getActive())){
            active = pageDto.getWhere().getActive();
        }

        //获取所有的device列表
        List<Device> deviceList = deviceMapper.findAll(pageDto, name, deviceKey, deviceTypeId, active);
        //查询总条数
        Long count = deviceMapper.count(name, deviceKey, deviceTypeId, active);
        //计算总页数
        Long pages = (count + pageDto.getSize() - 1) / pageDto.getSize();
        //封装数据
        return new PageResult<>(count,pages,deviceList);
    }
}
